﻿#include "FITKAbaqusAdaptorStepStatic.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITK_Kernel/FITKCore/FITKVec3D.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceModel/FITKAssInstanceTR.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceModel/FITKUnstructuredMesh.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepGeneralStatic.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"

namespace IO
{
    QString FITKAbaqusAdaptorStepStatic::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorStepStatic";
    }

    bool FITKAbaqusAdaptorStepStatic::adaptR()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepGeneralStatic*>(_dataObj);
        if (!_stepData || !_reader) return false;
        //读取静态分析的参数
        _reader->backLine();
        QString line = _reader->readLine();
        //校验
        if (!line.toLower().startsWith("*static")) return false;
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        //xxx关键字 ALLSDTOL/CONTINUE/DIRECT/FACTOR/FULLY PLASTIC/LONG TERM/RATE DEPENDENCE/RIKS/STABILIZE  如果是RIKS 则参数扩展到8个
        //是否绝热
        bool adiabatic = s.hasKey("adiabatic");
        //是否是固定增量
        bool direct = s.hasKey("direct");

        line = _reader->readLine();
        if (line.startsWith("*")) return false;
        auto sList = line.split(",");

        if (adiabatic)
        {
            //设置是否绝热， 目前数据结构不支持
            _reader->consoleMessage(3, "unsupported keyword caused length exception.");
            return false;
        }

        if (direct)
        {

            auto initialTimeIncrement = sList[0].toDouble();
            auto stepTime = sList[1].toDouble();
            _stepData->setFixedIncrementSize(initialTimeIncrement);
            _stepData->setTime(stepTime);
            _stepData->setIncrementType(Interface::FITKAbaAbstractStep::TimeIncrementType::Fixed);
        }

        if (sList.size() == 4)
        {
            auto initialTimeIncrement = sList[0].toDouble();
            auto stepTime = sList[1].toDouble();
            auto minimumTimeIncrement = sList[2].toDouble();
            auto maximumTimeIncrement = sList[3].toDouble();

            _stepData->setInitIncrementSize(initialTimeIncrement);
            _stepData->setTime(stepTime);
            _stepData->setMinIncrementSize(minimumTimeIncrement);
            _stepData->setMaxIncrementSize(maximumTimeIncrement);
            _stepData->setIncrementType(Interface::FITKAbaAbstractStep::TimeIncrementType::Automatic);

        }
        else if (sList.size() == 8)
        {
            _reader->consoleMessage(3, "unsupported keyword caused length exception.");
            return false;
        }
        else
        {
            _reader->consoleMessage(3, "unsupported keyword caused length exception.");
            return false;
        }
        return true;
    }

    bool FITKAbaqusAdaptorStepStatic::adaptW()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepGeneralStatic*>(_dataObj);
        if (!_stepData || !_writer) return false;
        // 写出节点函数主体
        QTextStream* stream = _writer->getStream();
        double initialTimeIncrement = _stepData->getInitIncrementSize();
        double stepTime = _stepData->getTime();
        double minimumTimeIncrement = _stepData->getMinIncrementSize();
        double maximumTimeIncrement = _stepData->getMaxIncrementSize();

        *stream << "*Static";

        //可能需要写出是否绝热

        if (_stepData->getIncrementType() == Interface::FITKAbaAbstractStep::TimeIncrementType::Fixed)
        {
            *stream << ", direct" << endl;
            *stream << _writer->double2String(initialTimeIncrement) << ",";
            *stream << _writer->double2String(stepTime) << ",";
        }
        else
        {
            *stream << endl;
            *stream << _writer->double2String(initialTimeIncrement, 8, 0);
            *stream << ", " << _writer->double2String(stepTime, 8, 0);
            *stream << ", " << _writer->double2String(minimumTimeIncrement, 8, 0);
            *stream << ", " << _writer->double2String(maximumTimeIncrement, 8, 0);
        }
        *stream << endl;
        return true;
    }
}
